package cc.sjfx.sharedoc.activity.examinfo;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.GetListener;
import cn.mandroid.frame.toast.MLog;
import cn.mandroid.frame.utils.ThreadUtils;
import cc.sjfx.sharedoc.R;
import cc.sjfx.sharedoc.bean.ExamInfo;
import cc.sjfx.sharedoc.db.bmob.DefaultInfo;
import cc.sjfx.sharedoc.dialog.LoadingDialog;
import cc.sjfx.sharedoc.fragment.BasicFragment;
import cc.sjfx.sharedoc.function.ReadAndWriteFile;
import cc.sjfx.sharedoc.utils.CheckUtils;
import cc.sjfx.sharedoc.utils.StrUtils;
import cc.sjfx.sharedoc.widget.adapter.MyExpandableAdapter;
import cc.sjfx.sharedoc.widget.view.myExpandableListView.MyExpandableListView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

public class ExamInfoFragment extends BasicFragment {
	private MyExpandableListView expandableListView;
	private Map<String, Map<String, String[]>> map = new HashMap<String, Map<String, String[]>>();
	private String[] groupData = new String[12];
	private MyExpandableAdapter adapter;
	private int expandFlag = -1;
	private LoadingDialog dialog;
	private int lastGroup = 0;
	private LayoutInflater inflater;
	@Override
	public View onCreateView(final LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		this.inflater = inflater;
		View view = inflater.inflate(R.layout.fragment_examinfo, container,
				false);
		dialog = new LoadingDialog(getActivity());
		expandableListView = (MyExpandableListView) view
				.findViewById(R.id.expandableListView1);
		dialog.show();
		ThreadUtils.executorService.execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				getData(inflater);
			}
		});
		return view;
	}

	private void getData(final LayoutInflater inflater) {
		// TODO Auto-generated method stub
		ReadAndWriteFile readAndWriteFile = new ReadAndWriteFile(getActivity());
		String data = readAndWriteFile.readFile(StrUtils.EXAMINFO_FILE);
		if (CheckUtils.stringIsNull(data)) {
			BmobQuery<DefaultInfo> query=new BmobQuery<DefaultInfo>();
			query.getObject(getActivity(), "McSWHHHI", new GetListener<DefaultInfo>() {
				@Override
				public void onFailure(int arg0, String arg1) {
					// TODO Auto-generated method stub
				}
				@Override
				public void onSuccess(DefaultInfo arg0) {
					// TODO Auto-generated method stub
					ReadAndWriteFile readAndWriteFile = new ReadAndWriteFile(
							getActivity());
					readAndWriteFile.writeFile(StrUtils.EXAMINFO_FILE,
							arg0.getContent());
					getData(inflater);
				}
			});
		} else if (ExamInfo.isNull) {
			try {
				if(ExamInfo.deal==null){
					ExamInfo.deal=new ExamInfoDeal(data);
					ExamInfo.deal.getExamInfos();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			handler.sendEmptyMessage(1);
		} else {
			handler.sendEmptyMessage(1);
		}
	}
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			expandableListView.setHeaderView(inflater.inflate(
					R.layout.myexpandablelistliew_topparent,
					expandableListView, false));
			expandableListView.setOnGroupExpandListener(groupExpandListener);
			adapter = new MyExpandableAdapter(ExamInfo.deal.setTopMonthMap(), ExamInfo.gd,
					getActivity(), ExamInfo.month, expandableListView);
			expandableListView.setAdapter(adapter);
			expandableListView.expandGroup(0);
			dialog.dismiss();
			super.handleMessage(msg);
		}
	};


	OnGroupExpandListener groupExpandListener = new OnGroupExpandListener() {

		@Override
		public void onGroupExpand(int arg0) {
			// TODO Auto-generated method stub
			expandableListView.setSelectedGroup(arg0);
			if (lastGroup != arg0) {
				expandableListView.collapseGroup(lastGroup);
			}
			lastGroup = arg0;
		}
	};

	class GroupClickListener implements OnGroupClickListener {
		@Override
		public boolean onGroupClick(ExpandableListView parent, View v,
				int groupPosition, long id) {
			MLog.i(expandFlag + ":" + groupPosition);
			if (expandFlag == -1) {
				// 展开被选的group
				expandableListView.expandGroup(groupPosition);
				// 设置被选中的group置于顶端
				expandableListView.setSelectedGroup(groupPosition);
				expandFlag = groupPosition;
			} else if (expandFlag == groupPosition) {
				expandableListView.collapseGroup(expandFlag);
				expandFlag = -1;
			} else {
				expandableListView.collapseGroup(expandFlag);
				// 展开被选的group
				expandableListView.expandGroup(groupPosition);
				// 设置被选中的group置于顶端
				expandableListView.setSelectedGroup(groupPosition);
				expandFlag = groupPosition;
			}
			return true;
		}
	}
}
